<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Answer extends Model
{
    /*回答问题API*/
    public function add(){
        if (!user_ins()->is_logged_in()){
            return ['status'=>0,'msg'=>'login require'];
        }
        if (!rq('question_id') || !rq('content'))
            return ['status'=>0,'msg'=>'question_id and content require'];
        $r = question_ins()->find(rq('question_id'));
        if (!$r) ['status'=>0,'msg'=>'question not exists'];

        $res = $this->where(['question_id'=>rq('question_id'),'user_id'=>session("user_id")])
            ->count();

        if ($res) return ['status'=>0,'msg'=>"duplicate answers"];


        $this->content = rq('content');
        $this->question_id = rq('question_id');
        $this->user_id = session("user_id");

        return $this->save()?
            ['status'=>1,'msg'=>'success','id'=>$this->id]:
            ['status'=>0,'msg'=>'answer question failed'];
    }
    /*更新回答API*/
    public function change(){
        if (!user_ins()->is_logged_in()) {
            return ['status'=>0,'msg'=>'login require'];
        }
        if (!rq('answers_id')){
            return ['status'=>0,'msg'=>'answers_id require'];
        }
        if (!rq('content')){
            return ['status'=>0,'msg'=>'content require'];
        }

        $res = $this->find(rq('answers_id'));
        if (!$res) return ['status'=>0,'msg'=>'answers not exists'];

        if ($res->user_id != session('user_id')){
            return ['status'=>0,'permission denied'];
        }

        $res->content = rq('content');
        $res->id = rq('answers_id');
        $res->user_id = session('user_id');

        return $res->save()?
            ['status'=>1,'msg'=>'answers update success']:
            ['status'=>0,'msg'=>'answers update failed'];

    }
    /*查看回答API*/
    public function read(){
        if (!rq('id') && !rq('question_id')){
            return ['status'=>0,'msg'=>['id or question_id is require']];
        }
        if (rq('id')){
            $answer = $this->find(rq('id'));

            if (!$answer){
                return ['status'=>0,'msg'=>'answers not exists'];
            }
            return ['status'=>0,'data'=>$answer];

        }
        if (!question_ins()->find(rq('question_id'))){
            return ['status'=>0,'msg'=>'question not exists'];
        }
        $answer = $this->where('question_id',rq('question_id'))
            ->get()
        ->keyBy('id');
        return ['status'=>1,'data'=>$answer];

    }
    /*投票API*/
    public function vote(){
        if (!user_ins()->is_logged_in()){
            return ['status'=>0,'msg'=>'login require'];
        }
        if (!rq('id') || !rq('vote')){
            return ['status'=>0,'msg'=>'id and vote require'];
        }

        $vote = rq('vote') <= 1 ? 1 : 2;

        $answer = $this->find(rq('id'));
        if (!$answer) return ['status'=>0,'msg'=>'answers not exists'];

        /*检查此用户是否投过票*/
        $answer->user()
            ->newPivotStatement()
            ->where('user_id',session('user_id'))
            ->where('answer_id',rq('id'))
            ->delete();

        $answer->user()->attach(session('user_id'),['vote'=>$vote]);

        return ['status'=>1];

    }

    public function user(){
        return $this
            ->belongsToMany('App\User')
            ->withPivot('vote')
            ->withTimestamps()
            ;
    }
    public function user_answer(){
        return $this->belongsTo("App\User");
    }
}
